package com.ddstation.crm.organization_structure.controller;

import com.ddstation.crm.accountManage.Entity.AccountManageEntity;
import com.ddstation.crm.accountManage.service.AccountManageService;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.login.util.WnSessionUtil;
import com.ddstation.crm.menu.entity.DtInvsManageInstitutionMenuEntity;
import com.ddstation.crm.menu.service.MenuService;
import com.ddstation.crm.organization_structure.entity.DtInvsOrganizationStructureEntity;
import com.ddstation.crm.organization_structure.entity.DtInvsOrganizationStructureStaffEntity;
import com.ddstation.crm.organization_structure.service.OrgService;
import com.ddstation.crm.organization_structure.service.StaffService;
import com.ddstation.security.util.WnBizMsgCrypt;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织架构 （组织架构管理）
 * @author zhangdifeng
 * @date 2018年1月9日
 */
@Controller @Lazy
public class OrgController {
	
	@RequestMapping(value="invs_system_config.do")
	public String initEvent(
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		String doctorUUID = WnBizMsgCrypt.encrypt(loginData.getDoctor().getSupervisorId().toString());
		model.addAttribute("doctorUUID", doctorUUID);
		//获取二级菜单目录
		String firstMenuUrl = "invs_system_config.do";
//		List<DtInvsManageInstitutionMenuEntity> secondMenus = menuService.getSecondMenus(loginData.getJurisdictionMenus(), firstMenuUrl);
		List<DtInvsManageInstitutionMenuEntity> secondMenus = menuService.getSecondMenus(loginData.getWorkbenchMenus(), firstMenuUrl);
		if(secondMenus == null){
			//无访问权限【跳转地址修改】
			return "forward:login?timeout=true";
		}
		List<DtInvsManageInstitutionMenuEntity> returnSecondMenus = null;
		//仅展示数据导入子模块
		for(DtInvsManageInstitutionMenuEntity secondMenu:secondMenus){
			if("config_data_import.do".equals(secondMenu.getUrl())){
				//如果存在则仅展示一个
				returnSecondMenus = new ArrayList<>();
				returnSecondMenus.add(secondMenu);
			}
		}
		if(returnSecondMenus == null){
			//无访问权限【跳转地址修改】
			return "forward:login?timeout=true";
		}
		model.addAttribute("secondMenus", returnSecondMenus);

		// 可用账号
//		String institutionId = WnSessionUtil.getInstitutionId(session);
//		List<AccountManageEntity> freeAccounts = accountManageService.getFreeAccount(institutionId);
//		model.addAttribute("freeAccounts", freeAccounts);
		return "pc/clinic/head_menu";
	}
	
	@RequestMapping(value="wncrm_org_struct.do/org/load", method=RequestMethod.POST)
	public @ResponseBody
    DtInvsOrganizationStructureEntity loadEvent(
			HttpSession session) {
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Integer doctorId = WnSessionUtil.getSupervisorId(session);
		return orgService.get(institutionId, doctorId);
	}
	
	@RequestMapping(value="wncrm_org_struct.do/org/add", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> addEvent(
			@RequestParam int pOrgId,
			@RequestParam String orgName,
			HttpSession session) {
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Map<String, Object> ret = new HashMap<String, Object>();
		DtInvsOrganizationStructureEntity orgStruct = new DtInvsOrganizationStructureEntity();
		orgStruct.setName(orgName);
		DtInvsOrganizationStructureEntity parent = new DtInvsOrganizationStructureEntity();
		parent.setId(pOrgId);
		orgStruct.setParent(parent);
		orgStruct.setInstitutionId(institutionId);
		if (orgService.add(orgStruct) == 1) {
			ret.put("errorcode", 0);
			ret.put("orgStruct", orgStruct);
		} else {
			ret.put("errorcode", -1);
		}
		return ret;
	}
	
	@RequestMapping(value="wncrm_org_struct.do/org/rename", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> renameEvent(
			@RequestParam int orgId,
			@RequestParam String orgName,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		DtInvsOrganizationStructureEntity orgStruct = new DtInvsOrganizationStructureEntity();
		orgStruct.setId(orgId);
		orgStruct.setName(orgName);
		orgStruct.setInstitutionId(WnSessionUtil.getInstitutionId(session));
		if (orgService.replace(orgStruct) == 1) {
			ret.put("errorcode", 0);
		} else {
			ret.put("errorcode", -1);
		}
		return ret;
	}
	
	@RequestMapping(value="wncrm_org_struct.do/org/remove", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> removeEvent(
			@RequestParam int orgId,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		List<DtInvsOrganizationStructureStaffEntity> coverStaffs = staffService.getCoverStaff(orgId);
		if (coverStaffs != null && !coverStaffs.isEmpty()) {
			ret.put("errorcode", 1); // 有成员，不能删除部门
		} else {
			DtInvsOrganizationStructureEntity orgStruct = new DtInvsOrganizationStructureEntity();
			orgStruct.setId(orgId);
			orgStruct.setInstitutionId(WnSessionUtil.getInstitutionId(session));
			if (orgService.remove(orgStruct) > 0) {
				ret.put("errorcode", 0);
			} else {
				ret.put("errorcode", -1);
			}
		}
		return ret;
	}

	private @Inject MenuService menuService;
	private @Inject OrgService orgService = null;
	private @Inject StaffService staffService = null;
	private @Inject AccountManageService accountManageService = null;
}
